package com.lizhi.note.repository;

import com.lizhi.note.entity.NotebookLastVisitEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Optional;

@Repository
public interface NotebookLastVisitRepository extends JpaRepository<NotebookLastVisitEntity, Long> {

    Optional<NotebookLastVisitEntity> findByNotebookIdAndDeletedFalse(Long notebookId);

    NotebookLastVisitEntity findByUserId(Long userId);

    Optional<NotebookLastVisitEntity> findByNotebookIdAndUserId(Long notebookId, Long userId);

    @Modifying
    @Transactional
    @Query("UPDATE NotebookLastVisitEntity n SET n.notebookId = :notebookId WHERE n.userId = :userId AND n.deleted = 0")
    void updateNotebookIdByUserId(Long notebookId, Long userId);
}
